﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

namespace MailingSystemWcfServiceRole.Entities
{
    public class Contact
    {
        public string StudentID { get; set; }
        public string StudentName { get; set; }

        //Calvin: should you change this into dropdown list, change data type accordingly. I recommend Enum
        public string School { get; set; }

        public string Email { get; set; }
        public string ContactNo { get; set; }
        
        //Calvin: same notes as School for these two
        public int Status { get; set; }
        public bool Registered { get; set; } //true = 1, false = 0, cuz you use bit as data type in the DB
    

        //STATIC METHODS
        public static bool ContactExists(string studId)
        {
            string connString = ConfigurationManager.ConnectionStrings["CalvinSQLDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand comm = new SqlCommand("SELECT StudentID from mailingList WHERE StudentID = @studId", conn);
            comm.Parameters.AddWithValue("@studId", studId);

            bool exists = false;

            try
            {
                SqlDataReader rdr = comm.ExecuteReader();
                if (!rdr.Read())
                {
                    exists = true;
                }
            }
            catch (Exception e)
            {
                //do nothing
            }
            finally
            {
                conn.Close();
            }

            return exists;
        }
    }
}